package cn.car91.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import cn.car91.config.InitApplication;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapCommonUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class ImageManager {
    private BitmapUtils utils = null;
    boolean clickLoadImg = false;

    public ImageManager(Context context) {
        init(context, "sdcard/car91/image");
    }

    public ImageManager(Context context, String path) {
        init(context, path);
    }

    public BitmapUtils getBitmapUtils() {
        return utils;
    }

    public void init(Context context, String path) {
        if (utils != null)
            return;
        utils = new BitmapUtils(context, path, 6 * 1024 * 1024, 1024 * 1024 * 50);

        utils.configDiskCacheEnabled(true);
        utils.configThreadPoolSize(5);
        utils.configMemoryCacheEnabled(true);
        utils.configDefaultBitmapMaxSize(BitmapCommonUtils.getScreenSize(context));

        if (CheckStatusUtil.getConnectedType(context) != ConnectivityManager.TYPE_WIFI)
            clickLoadImg = ((InitApplication) (context.getApplicationContext())).getSetPreferences().getBoolean("loadimg", true);
    }

    public void displayImage(final String url, final ImageView imageView, Drawable drawable) {
        final BitmapDisplayConfig config = new BitmapDisplayConfig();
        config.setLoadingDrawable(drawable);
        config.setLoadFailedDrawable(drawable);
        utils.configDefaultDisplayConfig(config);
        if (clickLoadImg) {
            imageView.setImageDrawable(drawable);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    utils.display(imageView, url, config);
                }
            });
        } else {
            utils.display(imageView, url, config);
        }
    }

    public void displayImage(String url, ImageView imageView,
                             final ImageListener listener) {
        if (listener != null) {
            utils.display(imageView, url, new BitmapLoadCallBack<ImageView>() {
                @Override
                public void onLoadFailed(ImageView arg0, String arg1,
                                         Drawable arg2) {
                    listener.onLoadingFailed(arg1, arg0);
                }

                @Override
                public void onLoadCompleted(ImageView imageView, String uri,
                                            Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig,
                                            BitmapLoadFrom bitmapLoadFrom) {
                    listener.onLoadingComplete(uri, imageView, bitmap);
                }
            });
        }
    }

    public void displayImage(final String url, final ImageView imageView,
                             final ImageListener listener, Drawable drawable) {
        if (listener != null) {
            BitmapDisplayConfig config = new BitmapDisplayConfig();
            config.setLoadingDrawable(drawable);
            config.setLoadFailedDrawable(drawable);
            if (clickLoadImg) {
                imageView.setImageDrawable(drawable);
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        utils.display(imageView, url, new BitmapLoadCallBack<View>() {
                            @Override
                            public void onLoadCompleted(View imageView, String uri,
                                                        Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig,
                                                        BitmapLoadFrom bitmapLoadFrom) {
                                listener.onLoadingComplete(uri, imageView, bitmap);
                            }

                            @Override
                            public void onLoadFailed(View arg0, String arg1,
                                                     Drawable arg2) {
                                listener.onLoadingFailed(arg1, arg0);
                            }
                        });
                    }
                });
            } else {
                utils.display(imageView, url, new BitmapLoadCallBack<View>() {
                    @Override
                    public void onLoadCompleted(View imageView, String uri,
                                                Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig,
                                                BitmapLoadFrom bitmapLoadFrom) {
                        listener.onLoadingComplete(uri, imageView, bitmap);
                    }

                    @Override
                    public void onLoadFailed(View arg0, String arg1,
                                             Drawable arg2) {
                        listener.onLoadingFailed(arg1, arg0);
                    }
                });
            }
        }
    }

    public void displayImage(final String url, final View view,
                             final ImageListener listener) {
        if (listener != null) {
            if (clickLoadImg) {
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        utils.display(view, url, new BitmapLoadCallBack<View>() {
                            @Override
                            public void onLoadCompleted(View imageView, String uri,
                                                        Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig,
                                                        BitmapLoadFrom bitmapLoadFrom) {
                                listener.onLoadingComplete(uri, imageView, bitmap);
                            }

                            @Override
                            public void onLoadFailed(View arg0, String arg1,
                                                     Drawable arg2) {
                                listener.onLoadingFailed(arg1, arg0);
                            }
                        });
                    }
                });
            } else {
                utils.display(view, url, new BitmapLoadCallBack<View>() {
                    @Override
                    public void onLoadCompleted(View imageView, String uri,
                                                Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig,
                                                BitmapLoadFrom bitmapLoadFrom) {
                        listener.onLoadingComplete(uri, imageView, bitmap);
                    }

                    @Override
                    public void onLoadFailed(View arg0, String arg1,
                                             Drawable arg2) {
                        listener.onLoadingFailed(arg1, arg0);
                    }
                });
            }
        }
    }

    public void displayImage(final String url, final ImageView imageView) {
        if (clickLoadImg) {
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    utils.display(imageView, url);
                }
            });
        } else {
            utils.display(imageView, url);
        }
    }

    public void displayImage(final String url, final View view, Drawable drawable) {
        final BitmapDisplayConfig config = new BitmapDisplayConfig();
        config.setLoadingDrawable(drawable);
        config.setLoadFailedDrawable(drawable);
        utils.configDefaultDisplayConfig(config);
        if (clickLoadImg) {
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    utils.display(view, url);
                }
            });
        } else {
            utils.display(view, url);
        }
    }

    public void displayImage(final String url, final View imageView) {
        if (clickLoadImg) {
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    utils.display(imageView, url);
                }
            });
        } else {
            utils.display(imageView, url);
        }
    }

    public interface ImageListener {
        public void onLoadingStarted(String uri, View view);

        public void onLoadingFailed(String uri, View view);

        public void onLoadingComplete(String uri, View view, Bitmap bitmap);
    }
}
